package com.nike.amoeba.adapter

import android.content.Context
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseExpandableListAdapter
import android.widget.TextView
import androidx.appcompat.widget.AppCompatImageView
import androidx.appcompat.widget.AppCompatTextView
import androidx.core.content.ContextCompat
import com.blankj.utilcode.util.LogUtils
import com.nike.amoeba.R
import com.nike.amoeba.bean.DataBean


class MainDrawerExpandableAdapter(
    private val context: Context,
    private val listBean: List<DataBean>
) : BaseExpandableListAdapter() {

    override fun getGroupCount(): Int = listBean.size

    override fun getChildrenCount(groupPosition: Int): Int {
        return listBean[groupPosition].ExpandableChild.size
    }

    override fun getGroup(groupPosition: Int): Any {
        return listBean[groupPosition]
    }

    override fun getChild(groupPosition: Int, childPosition: Int): Any {
        return listBean[groupPosition].ExpandableChild?.get(childPosition) ?: 0
    }

    override fun getGroupId(groupPosition: Int): Long = groupPosition.toLong()

    override fun getChildId(groupPosition: Int, childPosition: Int): Long = childPosition.toLong()

    override fun hasStableIds(): Boolean = false

    override fun getGroupView(
        groupPosition: Int,
        isExpanded: Boolean,
        convertView: View?,
        parent: ViewGroup
    ): View {
        val view: View = convertView ?: LayoutInflater.from(context).inflate(R.layout.i_expandable_group, parent, false)
        val bean = listBean[groupPosition]
        val tv_title: AppCompatTextView = view.findViewById(R.id.tv_title)
        val iv_image: AppCompatImageView = view.findViewById(R.id.iv_image)
        val img: AppCompatImageView = view.findViewById(R.id.img)
        when(groupPosition){
            0 -> img.background = ContextCompat.getDrawable(context, R.mipmap.icon_47)
            1 -> img.background = ContextCompat.getDrawable(context, R.mipmap.icon_48)
            2 -> img.background = ContextCompat.getDrawable(context, R.mipmap.icon_46)
            3 -> img.background = ContextCompat.getDrawable(context, R.mipmap.icon_42)
            4 -> img.background = ContextCompat.getDrawable(context, R.mipmap.icon_26)
            5 -> img.background = ContextCompat.getDrawable(context, R.mipmap.icon_41)
        }
        img.visibility = if (groupPosition == 6)View.GONE else View.VISIBLE


        iv_image.visibility = if (bean.Type <= 2){View.VISIBLE}else{View.GONE}
        tv_title.text = bean.Title

        iv_image.background = ContextCompat.getDrawable(context, if (isExpanded) R.mipmap.icon_49 else R.mipmap.icon_50)
        view.setBackgroundColor(
            if (bean.isSelect) {
                context.getColor(R.color.colorPrimaryDark)
            } else {
                Color.TRANSPARENT // 未选中时的背景色
            }
        )
        return view
    }

    override fun getChildView(
        groupPosition: Int,
        childPosition: Int,
        isLastChild: Boolean,
        convertView: View?,
        parent: ViewGroup
    ): View {
        val parentBean = listBean[groupPosition]
        val bean = parentBean.ExpandableChild.get(childPosition)
        val view: View = convertView ?: LayoutInflater.from(context).inflate(R.layout.i_expandable_child, parent, false)
        val tv_title: AppCompatTextView = view.findViewById(R.id.tv_title)
        tv_title.text = bean?.Title

        // 设置 Child 的背景颜色
        view.setBackgroundColor(
            if (bean.isSelect) {
                context.getColor(R.color.colorPrimaryDark)
            } else {
                Color.TRANSPARENT // 未选中时的背景色
            }
        )

        return view
    }

    override fun isChildSelectable(groupPosition: Int, childPosition: Int): Boolean = true

}